package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.Impl.CategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Author: Saka
 * Date: 2019/1/15
 * Time: 15:35
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryServiceImpl categoryService;

    /**
     * 根据父节点查询商品类目
     *
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryByParentId(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        List<Category> categories = this.categoryService.queryByParentId(pid);
        if (categories != null && 0 != categories.size()) {
            return ResponseEntity.ok(categories);

        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 回显商品分类信息
     *
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid) {
        List<Category> list = this.categoryService.queryByBrandId(bid);
        if (list != null && 0 != list.size()) {
            return ResponseEntity.ok(list);

        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据商品分类id查询名称
     *
     * @param ids
     * @return
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids) {
        List<String> list = this.categoryService.queryNamesByIds(ids);
        if (list != null && 0!=list.size()) {
            return ResponseEntity.ok(list);

        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();

    }

    /**
     *
     * 根据3级分类id,查询1-3级的分类
     * @param id
     * @return
     */
    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id") Long id) {
        List<Category> categories = this.categoryService.queryAllByCid3(id);
        if (categories != null && 0!=categories.size()) {
            return ResponseEntity.ok(categories);

        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
